<?php

/*

COMP 353F
Dr. B Desai
Final Project
Group #11

6330746 Nicholas CONSTANTINIDIS
9532862 Jacqueline FU
5484537 Claudio Javier LOPEZ FLORES
9218416 Que Tung NGUYEN

*/

  session_start();
?>

<!DOCTYPE html>
<html lang="en">
<head>
        <title>CoBAGSys - Home</title>
        <link type="text/css" rel="stylesheet" href="css/mystyle.css">
        <script type="text/JavaScript" src="js/validate.js"></script>
        <script type="text/JavaScript">
        <!--
        function ValidateFields() {
	        return ValidateFieldNotEmpty("paramForm", "d", "days before items are placed on garage sale")
	        && ValidateFieldNumeric("paramForm", "d", "days before items are placed on garage sale")
	        && ValidateFieldNotEmpty("paramForm", "a", "items reservable by non-members")
	        && ValidateFieldNumeric("paramForm", "a", "items reservable by non-members")
	        && ValidateFieldNotEmpty("paramForm", "m", "items reservable by members")
	        && ValidateFieldNumeric("paramForm", "m", "items reservable by members")
	        && ValidateFieldNotEmpty("paramForm", "n", "commission charged on transactions")
	        && ValidateFieldNumeric("paramForm", "n", "commission charged on transactions");
        }
        //-->
        </script>
</head>
<body>
    <?php
      require 'menus.php';
    ?>
    <?php
		// Perform the parameter set
		function setParameters() {
			$db = new Connect();
			
			$d = $_POST['d'];
			$a = $_POST['a'];
			$m = $_POST['m'];
			$n = $_POST['n'];
			  
			$sql = "UPDATE AdminSettings " .
				   "SET d = " . $d . ", " .
				   "a = " . $a . ", " .
				   "m = " . $m . ", " .
				   "n = " . $n;
				   
			
			$result = $db->query($sql);
			
			if(!$result || mysql_affected_rows() == 0) // Failed parameter set
			{
				return false;
			}
			else // New parameters set
			{
				return true;
			}
		}
		
		// Get existing parameter values
		function getParameters() {
			$db = new Connect();
			
			$sql = "SELECT * " .
				   "FROM AdminSettings";
				   
			$result = $db->query($sql);
			
			if(!$result || mysql_num_rows($result) == 0) // Couldn't find parameters
			{
				return;
			}
			else // Return parameter info
			{
				$rows = $db->to_array($result);
				return $rows[0];
			}
		}		
		
		// Check to see if we received post data and perform parameter set
		// TODO: Check for empty fields and valid parameter values
		$setparam_failed = false;
		if (isset($_POST["setparam"]))
			$setparam_failed = !setParameters();
    ?>
    <div id="main">
      	<p class="title">
      		Administration - Parameters
      	</p>
		<form id="paramForm" action="" onsubmit="return ValidateFields();" method="post">
<?php
	if ($setparam_failed == true)
		print "<p class=\"error\">FAILED SETTING NEW PARAMETERS!</p>";

	$params = getParameters();
	if (isset($params))
	{
		print "<p class=\"normal\">";
		print "<input type=\"text\" maxlength=\"2\" size=\"2\" name=\"d\" value=\"" . $params['d'] . "\">";
		print "&nbsp days before items are placed on garage sale.";
		print "</p>";
		print "<p class=\"normal\">";
		print "<input type=\"text\" maxlength=\"2\" size=\"2\" name=\"a\" value=\"" . $params['a'] . "\">";
		print "&nbsp items reservable by non-members.";
		print "</p>";
		print "<p class=\"normal\">";
		print "<input type=\"text\" maxlength=\"2\" size=\"2\" name=\"m\" value=\"" . $params['m'] . "\">";
		print "&nbsp items reservable by members.";
		print "</p>";
		print "<p class=\"normal\">";
		print "<input type=\"text\" maxlength=\"2\" size=\"2\" name=\"n\" value=\"" . $params['n'] . "\">";
		print " % commission charged on transactions.";
		print "</p>";
	}
	else
	{
		print "<p class=\"normal\">";
		print "<input type=\"text\" maxlength=\"2\" size=\"2\" name=\"d\" placeholder=\"##\">";
		print "&nbsp days before items are placed on garage sale.";
		print "</p>";
		print "<p class=\"normal\">";
		print "<input type=\"text\" maxlength=\"2\" size=\"2\" name=\"a\" placeholder=\"##\">";
		print "&nbsp items reservable by non-members.";
		print "</p>";
		print "<p class=\"normal\">";
		print "<input type=\"text\" maxlength=\"2\" size=\"2\" name=\"m\" placeholder=\"##\">";
		print "&nbsp items reservable by members.";
		print "</p>";
		print "<p class=\"normal\">";
		print "<input type=\"text\" maxlength=\"2\" size=\"2\" name=\"n\" placeholder=\"##\">";
		print " % commission charged on transactions.";
		print "</p>";	
	}
?>
			<input type="submit" class="largeButton" name="setparam" value="Set parameters"/>
		</form>
    </div>
</body>
</html>